package com.java.service.impl;

import com.java.entity.PageBean;
import com.java.entity.PsInspectiontaskMain;
import com.java.mapper.PsInspectiontaskMainMapper;
import com.java.service.PsInspectiontaskMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author caiyuan
 * @version 1.0
 * @Date 2020/6/20 12:53 Dream-Seeker
 * @Description
 */
@Service
public class PsInspectiontaskMainServiceImpl implements PsInspectiontaskMainService {
    @Autowired
    PsInspectiontaskMainMapper psInspectiontaskMainMapper;

    @Override
    public PageBean<PsInspectiontaskMain> findByPageInspec(Integer currentPage,
                                                           String inspectionTaskCode,
                                                           String lineCode,
                                                           String taskStatusName,
                                                           String issuedByName,
                                                           String issuedTimestart,
                                                           String issuedTimeend) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        PageBean<PsInspectiontaskMain> pageBean = new PageBean<PsInspectiontaskMain>();

        //每页显示的数据
        int pageSize = 5;
        pageBean.setPageSize(pageSize);

        //封装总记录数
        int totalCount = psInspectiontaskMainMapper.selectCount(
                inspectionTaskCode,
                lineCode,
                taskStatusName,
                issuedByName,
                issuedTimestart,
                issuedTimeend);
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());

        if (currentPage > pageBean.getTotalPage()) {
            //封装当前页数
            pageBean.setCurrPage(pageBean.getTotalPage());
        } else if (currentPage < 0) {
            pageBean.setCurrPage(1);
        } else {
            pageBean.setCurrPage(currentPage);
        }
        map.put("start", (currentPage - 1) * pageSize);
        map.put("size", pageBean.getPageSize());
        map.put("inspectionTaskCode", inspectionTaskCode);
        map.put("lineCode", lineCode);
        map.put("taskStatusName", taskStatusName);
        map.put("issuedByName", issuedByName);
        map.put("issuedTimestart", issuedTimestart);
        map.put("issuedTimeend", issuedTimeend);
        //封装每页显示的数据
        List<PsInspectiontaskMain> lists = psInspectiontaskMainMapper.findByPageInspec(map);
        pageBean.setLists(lists);
        return pageBean;
    }


    @Override
    public PsInspectiontaskMain getInspecById(Integer InspecId) {
        return psInspectiontaskMainMapper.getInspecById(InspecId);
    }

    @Override
    public Integer addInspection(PsInspectiontaskMain psInspectiontaskMain) {
        psInspectiontaskMain.setTaskStatusName("待分配");
        psInspectiontaskMain.setTaskStatus(1);
        psInspectiontaskMain.setIsCancel("否");
        if (psInspectiontaskMain.getIssuedByName().equals("admin")) {
            psInspectiontaskMain.setIssuedByCode("2");
        }
        return psInspectiontaskMainMapper.addInspection(psInspectiontaskMain);
    }

    @Override
    public Integer updatePsInspection(PsInspectiontaskMain psInspectiontaskMain) {
        return psInspectiontaskMainMapper.updatePsInspection(psInspectiontaskMain);
    }

    @Override
    public Integer delInspection(Integer inspeId) {
        return psInspectiontaskMainMapper.delInspection(inspeId);
    }

    @Override
    public Integer taskStatusInspection(Integer inspeId) {
        return psInspectiontaskMainMapper.taskStatusInspection(inspeId);
    }

    @Override
    public Integer carriedOutInspection(Integer inspeId) {
        return psInspectiontaskMainMapper.carriedOutInspection(inspeId);
    }




}
